﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Collections.Specialized;

namespace CP.Repositories.GPM
{
    public class ColumnListRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Column_List> GetAllColumn_Lists(int webpartid)
        {
            return db.ExecuteObject<Column_List>(string.Format(@"select * from Column_List 
                                where webpart_id = {0} order by List_id", webpartid));
        }

        public Column_List GetColumn_ListByID(int webpartid, int colid)
        {
            IList<Column_List> Column_Lists = db.ExecuteObject<Column_List>(string.Format(@"select * 
                                                from Column_List where webpart_id = {0}
                                                and list_id = {1} ", webpartid, colid));
            if (Column_Lists.Count > 0)
                return Column_Lists[0];
            return null;
        }

        public bool DeleteColumn_List(int webpartid, IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"delete Column_List 
                                        where webpart_id = {0}
                                          and list_id = {1} ", webpartid, id));
                    db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_LIST 
                                        where webpart_id = {0}
                                          and list_id = {1} ", webpartid, id));
                }
                db.Commit();
                return true;
            }

            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool DeleteColumn_ListByID(int webpartid, int colid)
        {
            object ret = db.ExecuteScalar(string.Format(@"select count(*) from dual
                                           where exists(select null from 
                                           column_list where webpart_id = {0}
                                                    and list_id = {1} )",
                                           webpartid, colid));

            if (0 == Convert.ToInt16(ret))
                throw new Exception("列ID不存在");

            db.ExecuteNonQuery(string.Format(@"delete Column_List 
                                        where webpart_id = {0}
                                          and list_id = {1} ", webpartid, colid));
            db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_LIST 
                                        where webpart_id = {0}
                                          and list_id = {1} ", webpartid, colid));
            return true;
        }

        public bool DeleteColumn_ListByWebpartID(int webpartid)
        {
            db.ExecuteNonQuery(string.Format(@"delete Column_List 
                                        where webpart_id = {0} ", webpartid));
            db.ExecuteNonQuery(string.Format(@"delete WEBPART_RIGHT_LIST 
                                        where webpart_id = {0} ", webpartid));
            return true;
        }

        public bool AddColumn_List(int webpartid, Column_List list)
        {
            list.list_id = GetListIDBySeq();
            object ret = db.ExecuteScalar(string.Format(@"select count(*) from dual
                                           where exists(select null from 
                                           Column_List  where webpart_id = {0}
                                            and list_id = {1} )",
                                     webpartid, list.list_id));

            if (0 != Convert.ToInt16(ret))
                throw new Exception("列ID已经存在");

            db.ExecuteNonQuery(string.Format(@"insert into Column_List(list_id,field_name,
                                        list_name,webpart_id,grid_name) 
                                        values({0},'{1}','{2}',{3},'{4}') ",
                                list.list_id, list.field_name, list.list_name,
                                webpartid,list.grid_name));
            return true;
        }

        public bool UpdateColumn_List(int webpartid, Column_List list)
        {
            object ret = db.ExecuteScalar(string.Format(@"select count(*) from dual
                                           where exists(select null from 
                                           column_list where webpart_id = {0}
                                                    and list_id = {1} )",
                                          webpartid, list.list_id));

            if (0 == Convert.ToInt16(ret))
                throw new Exception("按钮编号不存在");

            db.ExecuteNonQuery(string.Format(@"update Column_List set list_name='{1}',
                                            field_name= '{2}',grid_name = '{4}'
                                            where webpart_id={3} and list_id = {0} ",
                               list.list_id, list.list_name, list.field_name, webpartid,list.grid_name));
            return true;
        }

        public int GetListIDBySeq()
        {
            return SequenceGenerator.GetSeqNextValue("COL_LIST_SEQ");
        }
    }
}
